package Test;

import java.util.Stack;

public class 有效的括号 {
    public static boolean isValid(String s) {
        Stack<Character> s1=new Stack<>();
        for (char c:s.toCharArray()){
            if(c=='('||c=='{'||c=='['){
                s1.push(c);
            }else if(!s1.isEmpty()&&left(c)==s1.peek()){
                s1.pop();
            }
            else {
                return false;
            }
        }
        return s1.isEmpty();
    }

    static char left(char c){
        if(c=='}') return '{';
        else if(c==']') return '[';
        else return '(';
    }
    public static void main(String[] args) {
        System.out.println("isValid(\"(){}\") = " + isValid("(){}"));
    }
}
